package org.social.db.dao;

import com.sojoys.artifact.manager.DataSourceManager;
import java.sql.Connection;
import com.sojoys.artifact.build.data.base.BaseDao;
import java.util.ArrayList;
import com.sojoys.artifact.manager.JsonManager;
import java.sql.SQLException;
import com.google.common.collect.Lists;
import org.apache.commons.dbutils.handlers.MapHandler;
import com.sojoys.artifact.tools.SK_Plus;
import java.sql.ResultSet;
import java.util.Map;
import javax.sql.DataSource;
import org.social.db.bean.WechatShopOrder;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sojoys.artifact.constant.CreateTableEnum;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import org.apache.commons.dbutils.handlers.AbstractListHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.xiaoleilu.hutool.log.Log;
import java.util.Set;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import static org.social.db.StaticResource.WechatShopOrderResource.*;
import com.xiaoleilu.hutool.util.StrUtil;
import com.sojoys.artifact.tools.ToolError;
import org.apache.commons.dbutils.DbUtils;
import com.xiaoleilu.hutool.log.LogFactory;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.dbutils.ResultSetHandler;
import com.google.common.base.Joiner;
import static org.social.db.StaticResource.WechatShopOrderResource.*;
/**
 * 微信积分兑换商城
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class WechatShopOrderDaoImpl implements BaseDao<String,WechatShopOrder>{
	static Log log = LogFactory.get(WechatShopOrderDaoImpl.class);
	
	// 代理
	
	/**
	 * 私有化无参构造方法
	 */
	private WechatShopOrderDaoImpl() {}
	  
    private static final WechatShopOrderDaoImpl INSTANCE = new WechatShopOrderDaoImpl();  
    
    public static WechatShopOrderDaoImpl me() {  
        return INSTANCE;  
    }  
	
	public WechatShopOrder insert(WechatShopOrder wechatShopOrder){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(wechatShopOrder,conn);
	}
	
	public WechatShopOrder insert(WechatShopOrder wechatShopOrder,Connection conn){
		return insert(wechatShopOrder,conn,TABLE_NAME);
	}
	
	public WechatShopOrder insert(WechatShopOrder wechatShopOrder,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(wechatShopOrder,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public WechatShopOrder insert(WechatShopOrder wechatShopOrder,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(wechatShopOrder,conn,tableName);
	}
	
	public WechatShopOrder insert(WechatShopOrder wechatShopOrder,Connection conn,String tableName){
		
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (orderid,serverId,playerId,prizeid,time,success,create_date,modified_date) VALUES (?,?,?,?,?,?,?,?)";
		try {
			int i = run.update(conn,sql,wechatShopOrder.getOrderid(),wechatShopOrder.getServerId(),wechatShopOrder.getPlayerId(),wechatShopOrder.getPrizeid(),wechatShopOrder.getTime(),wechatShopOrder.isSuccess(),wechatShopOrder.getCreateDate(),wechatShopOrder.getModifiedDate());
			return i > 0 ? wechatShopOrder : null;
		} catch (Exception e) {
			log.error(e);
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public WechatShopOrder insert(WechatShopOrder wechatShopOrder,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(wechatShopOrder,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public int[] insertBatch(List<WechatShopOrder> wechatShopOrders){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(wechatShopOrders,conn);
	}
	
	public int[] insertBatch(List<WechatShopOrder> wechatShopOrders,Connection conn){
		return insertBatch(wechatShopOrders,conn,TABLE_NAME);
	}
	
	public int[] insertBatch(List<WechatShopOrder> wechatShopOrders,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(wechatShopOrders,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] insertBatch(List<WechatShopOrder> wechatShopOrders,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(wechatShopOrders,conn,tableName);
	}
	
	public int[] insertBatch(List<WechatShopOrder> wechatShopOrders,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (orderid,serverId,playerId,prizeid,time,success,create_date,modified_date) VALUES (?,?,?,?,?,?,?,?)";
		try {
			int columnSize = 8;
			int size = wechatShopOrders.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =wechatShopOrders.get(i).getOrderid();
				params[i][1] =wechatShopOrders.get(i).getServerId();
				params[i][2] =wechatShopOrders.get(i).getPlayerId();
				params[i][3] =wechatShopOrders.get(i).getPrizeid();
				params[i][4] =wechatShopOrders.get(i).getTime();
				params[i][5] =wechatShopOrders.get(i).isSuccess();
				params[i][6] =wechatShopOrders.get(i).getCreateDate();
				params[i][7] =wechatShopOrders.get(i).getModifiedDate();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] insertBatch(List<WechatShopOrder> wechatShopOrders,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(wechatShopOrders,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public WechatShopOrder update(WechatShopOrder wechatShopOrder){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(wechatShopOrder,conn);
	}
	
	public WechatShopOrder update(WechatShopOrder wechatShopOrder,Connection conn){
		return update(wechatShopOrder,conn,TABLE_NAME);
	}
	
	public WechatShopOrder update(WechatShopOrder wechatShopOrder,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(wechatShopOrder,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public WechatShopOrder update(WechatShopOrder wechatShopOrder,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(wechatShopOrder,conn,tableName);
	}
	
	public WechatShopOrder update(WechatShopOrder wechatShopOrder,Connection conn,String tableName){
		List<WechatShopOrder> wechatShopOrders = new ArrayList<WechatShopOrder>();
		wechatShopOrders.add(wechatShopOrder);
		updateBatch(wechatShopOrders,conn,tableName);
		return wechatShopOrder;
	}
	
	public WechatShopOrder update(WechatShopOrder wechatShopOrder,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(wechatShopOrder,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<WechatShopOrder> wechatShopOrders){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(wechatShopOrders,conn);
	}
	
	public int[] updateBatch(List<WechatShopOrder> wechatShopOrders,Connection conn){
		return updateBatch(wechatShopOrders,conn,TABLE_NAME);
	}
	
	public int[] updateBatch(List<WechatShopOrder> wechatShopOrders,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(wechatShopOrders,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<WechatShopOrder> wechatShopOrders,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(wechatShopOrders,conn,tableName);
	}
	
	public int[] updateBatch(List<WechatShopOrder> wechatShopOrders,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET orderid = ?,serverId = ?,playerId = ?,prizeid = ?,time = ?,success = ?,create_date = ?,modified_date = ? WHERE orderid = ?";
		try {
			int columnSize = 8;
			int size = wechatShopOrders.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =wechatShopOrders.get(i).getOrderid();
				params[i][1] =wechatShopOrders.get(i).getServerId();
				params[i][2] =wechatShopOrders.get(i).getPlayerId();
				params[i][3] =wechatShopOrders.get(i).getPrizeid();
				params[i][4] =wechatShopOrders.get(i).getTime();
				params[i][5] =wechatShopOrders.get(i).isSuccess();
				params[i][6] =wechatShopOrders.get(i).getCreateDate();
				params[i][7] =wechatShopOrders.get(i).getModifiedDate();
				params[i][columnSize] =wechatShopOrders.get(i).getOrderid();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] updateBatch(List<WechatShopOrder> wechatShopOrders,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(wechatShopOrders,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean delete(WechatShopOrder wechatShopOrder){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(wechatShopOrder,conn);
	}
	
	public boolean delete(WechatShopOrder wechatShopOrder,Connection conn){
		return delete(wechatShopOrder,conn,TABLE_NAME);
	}
	
	public boolean delete(WechatShopOrder wechatShopOrder,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(wechatShopOrder,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean delete(WechatShopOrder wechatShopOrder,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(wechatShopOrder,conn,tableName);
	}
	
	public boolean delete(WechatShopOrder wechatShopOrder,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE orderid = ?";
		try {
			int i = run.update(conn,sql, wechatShopOrder.getOrderid());
			return i > 0 ? true : false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean delete(WechatShopOrder wechatShopOrder,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(wechatShopOrder,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public boolean deleteBatch(List<WechatShopOrder> wechatShopOrders){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(wechatShopOrders,conn);
	}
	
	public boolean deleteBatch(List<WechatShopOrder> wechatShopOrders,Connection conn){
		return deleteBatch(wechatShopOrders,conn,TABLE_NAME);
	}
	
	public boolean deleteBatch(List<WechatShopOrder> wechatShopOrders,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(wechatShopOrders,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean deleteBatch(List<WechatShopOrder> wechatShopOrders,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(wechatShopOrders,conn,tableName);
	}
	
	public boolean deleteBatch(List<WechatShopOrder> wechatShopOrders,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE orderid = ?";
		try {
			int size = wechatShopOrders.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = wechatShopOrders.get(i).getOrderid();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean deleteBatch(List<WechatShopOrder> wechatShopOrders,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(wechatShopOrders,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}

	/**
	 * 根据( orderid ) 查询
	 */
	public WechatShopOrder getByOrderid(String orderid){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByOrderid(orderid, conn);
	}
	
	public WechatShopOrder getByOrderid(String orderid,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByOrderid(orderid, conn,tableName);
	}
	public List<WechatShopOrder> getIn(Set<String> orderids){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(orderids, conn);
	}
	
	public List<WechatShopOrder> getIn(Set<String> orderids,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(orderids, conn,tableName);
	}
	
	//Connection
	/**
	 * 根据( orderid ) 查询
	 */
	public WechatShopOrder getByOrderid(String orderid,Connection conn){
		return getByOrderid(orderid,conn,TABLE_NAME);
	}
	
	public WechatShopOrder getByOrderid(String orderid,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT orderid,serverId,playerId,prizeid,time,success,create_date,modified_date FROM " + tableName + " WHERE " + "orderid = ? ORDER BY orderid ASC LIMIT 1";
		WechatShopOrder wechatShopOrder = null; 
		try {
			wechatShopOrder = run.query(conn,sql, new WechatShopOrderHandler(), orderid);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return wechatShopOrder;
	}
	public List<WechatShopOrder> getIn(Set<String> orderids,Connection conn){
		return getIn(orderids,conn,TABLE_NAME);
	}
	
	public List<WechatShopOrder> getIn(Set<String> orderids,Connection conn,String tableName){
		List<WechatShopOrder> wechatShopOrders = Lists.newArrayList();
		if(orderids.isEmpty()) return wechatShopOrders;
		QueryRunner run = new QueryRunner();
		String sql = "SELECT orderid,serverId,playerId,prizeid,time,success,create_date,modified_date FROM " + tableName + " WHERE " + "orderid in (%s) ORDER BY orderid ASC";
		sql = String.format(sql, Joiner.on(",").join(orderids));
		try {
			wechatShopOrders = run.query(conn,sql, new WechatShopOrderListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return wechatShopOrders;
	}
	
	//DataSource
	/**
	 * 根据( orderid ) 查询
	 */
	public WechatShopOrder getByOrderid(String orderid,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByOrderid(orderid, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public WechatShopOrder getByOrderid(String orderid,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByOrderid(orderid, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	public List<WechatShopOrder> getIn(Set<String> orderids,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getIn(orderids, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<WechatShopOrder> getIn(Set<String> orderids,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getIn(orderids, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	
	public List<WechatShopOrder> getAll(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn);
	}
	
	public List<WechatShopOrder> getAll(Connection conn){
		return getAll(conn,TABLE_NAME);
	}
	
	public List<WechatShopOrder> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<WechatShopOrder> getAll(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn,tableName);
	}
	
	public List<WechatShopOrder> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT orderid,serverId,playerId,prizeid,time,success,create_date,modified_date FROM " + tableName + " ORDER BY orderid ASC";
		List<WechatShopOrder> wechatShopOrders = Lists.newArrayList();
		try {
			wechatShopOrders = run.query(conn, sql, new WechatShopOrderListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return wechatShopOrders;
	}
	
	public List<WechatShopOrder> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public List<WechatShopOrder> getAllPage(int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,page,pageSize);
	}
	
	public List<WechatShopOrder> getAllPage(Connection conn,int page,int pageSize){
		return getAllPage(conn,TABLE_NAME,page,pageSize);
	}
	
	public List<WechatShopOrder> getAllPage(DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<WechatShopOrder> getAllPage(String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,tableName,page,pageSize);
	}
	
	public List<WechatShopOrder> getAllPage(Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT orderid,serverId,playerId,prizeid,time,success,create_date,modified_date FROM " + tableName + " ORDER BY orderid ASC LIMIT " + page + " , " +pageSize;
		List<WechatShopOrder> wechatShopOrders = Lists.newArrayList();
		try {
			wechatShopOrders = run.query(conn, sql, new WechatShopOrderListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return wechatShopOrders;
	}
	
	public List<WechatShopOrder> getAllPage(DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean truncate(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn);
	}
	
	public boolean truncate(Connection conn){
		return truncate(conn,TABLE_NAME);
	}
	
	public boolean truncate(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean truncate(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn,tableName);
	}
	
	public boolean truncate(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "TRUNCATE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean truncate(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	// 执行自定义查询sql
	public List<Map<String,Object>> executeQuerySql(String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String sql){
		return executeQuerySql(conn,TABLE_NAME,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String sql){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,sql);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Map<String,Object>> executeQuerySql(String tableName,String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,tableName);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String tableName,String sql){
		QueryRunner run = new QueryRunner();
		ToolError.isAndTrue(ArtifactErrorCode.SQL_PERMISSIONS_ERROR, !StrUtil.startWithIgnoreCase(sql, "SELECT"));
		List<Map<String,Object>> list = Lists.newArrayList();
		try {
			list = run.query(conn,sql, new MapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return list;
	}
	/**
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	*/
	
	//
	
	//
	
	//Drop Table
	public boolean drop(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn);
	}
	
	public boolean drop(Connection conn){
		return drop(conn,TABLE_NAME);
	}
	
	public boolean drop(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return drop(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean drop(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn,tableName);
	}
	
	public boolean drop(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DROP TABLE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean drop(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return drop(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	//create
	public boolean createTable(CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,type);
	}
	
	public boolean createTable(Connection conn,CreateTableEnum type){
		return createTable(conn,TABLE_NAME,type);
	}
	
	public boolean createTable(DataSource ds,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean createTable(String tableName,CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,tableName,type);
	}
	
	public boolean createTable(Connection conn,String tableName,CreateTableEnum type){
		QueryRunner run = new QueryRunner();
		SK_Plus plus = SK_Plus.b("CREATE TABLE IF NOT EXISTS `", tableName,"` (");
		plus.a("  `orderid` VARCHAR(15) NOT NULL  COMMENT 'remarks=订单号',");	
		plus.a("  `serverId` VARCHAR(45)   COMMENT 'remarks=区服id',");	
		plus.a("  `playerId` INT(11)   COMMENT 'remarks=角色id',");	
		plus.a("  `prizeid` INT(11)   COMMENT 'remarks=礼包id',");	
		plus.a("  `time` DATETIME   COMMENT 'remarks=下单时间',");
		plus.a("  `success` TINYINT(1)   COMMENT 'remarks=订单处理状态 0-失败 1-成功',");	
		plus.a("  `create_date` DATETIME   COMMENT '',");
		plus.a("  `modified_date` DATETIME   COMMENT '',");
		plus.a("  PRIMARY KEY (`orderid`),");
		if (CreateTableEnum.LOGIC.equals(type) || CreateTableEnum.DESIGN.equals(type)) {
			
		}
		if (CreateTableEnum.DESIGN.equals(type)) {
		}
		String sql = plus.e();
		sql = StrUtil.subPre(sql, sql.lastIndexOf(StrUtil.C_COMMA));
		sql+=") ENGINE=InnoDB DEFAULT CHARSET=utf8  COMMENT='remarks=微信积分兑换商城';";
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean createTable(DataSource ds,String tableName,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,tableName,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public String getTableName(){
		return TABLE_NAME;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getColumnsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"wechat_shop_order\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"orderid\",\"NULLABLE\":0,\"REMARKS\":\"remarks=订单号\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":45,\"ORDINAL_POSITION\":1,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":15}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"wechat_shop_order\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"serverId\",\"NULLABLE\":1,\"REMARKS\":\"remarks=区服id\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":135,\"ORDINAL_POSITION\":2,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":45}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"wechat_shop_order\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"playerId\",\"NULLABLE\":1,\"REMARKS\":\"remarks=角色id\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":3,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"wechat_shop_order\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"prizeid\",\"NULLABLE\":1,\"REMARKS\":\"remarks=礼包id\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":4,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"wechat_shop_order\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"time\",\"NULLABLE\":1,\"REMARKS\":\"remarks=下单时间\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":5,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"wechat_shop_order\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"success\",\"NULLABLE\":1,\"REMARKS\":\"remarks=订单处理状态 0-失败 1-成功\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":6,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":-6,\"TYPE_NAME\":\"TINYINT\",\"COLUMN_SIZE\":1}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"wechat_shop_order\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"create_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":7,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"wechat_shop_order\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"modified_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":8,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getIndexsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_social_design\",\"PAGES\":null,\"TABLE_NAME\":\"wechat_shop_order\",\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"orderid\",\"INDEX_NAME\":\"PRIMARY\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_social_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"FILTER_CONDITION\":null,\"TYPE\":3}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
//自定义内容起始位置
class WechatShopOrderHandler implements ResultSetHandler<WechatShopOrder>{
	@Override
	public WechatShopOrder handle(ResultSet rs) throws SQLException {
		return rs.next() ? WechatShopOrder.builder().createForResultSet(rs).syncIndexValues() : null ;
	}
}

class WechatShopOrderListHandler extends AbstractListHandler<WechatShopOrder>{
	@Override
	protected WechatShopOrder handleRow(ResultSet rs) throws SQLException {
		return WechatShopOrder.builder().createForResultSet(rs).syncIndexValues();
	}
}

//自定义内容结束位置
}

